package com.golddaymicromall.activity.setting.personal.bankcard;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.application.MyApplication;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.bean.BankInfoshow;
import com.golddaymicromall.util.IsNetUtils;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.golddaymicromall.view.DragDelItem;
import com.golddaymicromall.view.DragDelListView;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

public class Card_mybankActivity extends BaseActivity implements View.OnClickListener {
    private List<BankInfoshow> list = new ArrayList<BankInfoshow>();
    private List<BankInfoshow> listAll = new ArrayList<BankInfoshow>();
    private ArrayList<String> list1 = new ArrayList<String>();
    private DragDelListView mlv;
    private Button mnewbankcard;
    private String customerid;
    private AppAdapter Adapter;
    private LinearLayout mMyBankCardBack;
    private String Customerno;
    private IsNetUtils isnet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_mybank);
        isnet = new IsNetUtils(Card_mybankActivity.this);
        initView();
        Customerno = SPUtils.get(getApplicationContext(), "Customerno", "").toString();
        attemptGetOnlineRemittance();
    }

    private void initView() {
        mlv = (DragDelListView) findViewById(R.id.banklv);
        mnewbankcard = (Button) findViewById(R.id.newbankcard1);
        mMyBankCardBack = (LinearLayout) findViewById(R.id.mybankcard_back);
        mMyBankCardBack.setOnClickListener(this);
        mnewbankcard.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        attemptGetOnlineRemittance();
    }

    public void attemptGetOnlineRemittance() {
        String logid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.bank.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("customerid", logid);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    if (!"".equals(s)) {
                        if (s.contains("errorToken") || "0".equals(s)) {
                            Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(s)) {
                            Toast.makeText(getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            list = JSON.parseArray(s, BankInfoshow.class);
                            Adapter = new AppAdapter();
                            mlv.setAdapter(Adapter);
                            Adapter.notifyDataSetChanged();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                });

    }

    public void attemptdelecte(String id, int posion) {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.bank.delete");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("id", id);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    if (!"".equals(s)) {
                        if (s.contains("errorToken") || "0".equals(s)) {
                            Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(s)) {
                            Toast.makeText(getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            if (Integer.parseInt(s) == 1) {
                                Toast.makeText(Card_mybankActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                list.remove(posion);
                                Adapter.notifyDataSetChanged();
                                MyApplication.getInstance().attemptRiZhi(Customerno, "银行卡删除");
                            } else {
                                Toast.makeText(Card_mybankActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.newbankcard1:
                Intent intent = new Intent(this, NewAddBankCardActivity.class);
                startActivity(intent);
                break;
            case R.id.mybankcard_back:
                finish();
                break;

        }
    }



    class AppAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            final int loc = position;
            ViewHolder holder = null;
            View menuView = null;
            if (convertView == null) {


                convertView = View.inflate(getApplicationContext(),
                        R.layout.item_activity_cart_mybank, null);

                menuView = View.inflate(getApplicationContext(),
                        R.layout.swipemenu, null);

                convertView = new DragDelItem(convertView, menuView);
                holder = new ViewHolder(convertView);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.mbank_number.setText("卡号：" + list.get(position).getBankNo());
            holder.mbank_name.setText("银行：" + list.get(position).getName());
            if (list.get(position).getStatus().equals("0")) {
                holder.bank_name_def.setText("【默认】");
            } else {
                holder.bank_name_def.setText("");
            }
            holder.tv_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    attemptdelecte(list.get(position).getId(), position);
                    notifyDataSetChanged();
                }
            });

            holder.tv_updete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!isnet.IsNet()) {
                        Toast.makeText(Card_mybankActivity.this, "网络漫游中请查看是否有网", Toast.LENGTH_SHORT).show();

                    } else {
                        Intent intent = new Intent(getApplicationContext(), BankUpDateActivity.class);
                        if (list.size() > 0) {
                            intent.putExtra("id", list.get(position).getId());
                            intent.putExtra("BankProvince", list.get(position).getBankProvince());
                            intent.putExtra("BankCity", list.get(position).getBankCity());
                            intent.putExtra("BankArea", list.get(position).getBankArea());
                            intent.putExtra("BankName", list.get(position).getName());
                            intent.putExtra("Status", list.get(position).getStatus());
                        }
                        startActivity(intent);
                    }

                }
            });

            return convertView;
        }

        class ViewHolder {
            TextView mbank_number;
            TextView mbank_name;
            TextView tv_updete, tv_del;
            TextView bank_name_def;

            public ViewHolder(View view) {
                mbank_number = (TextView) view.findViewById(R.id.bank_number);
                mbank_name = (TextView) view.findViewById(R.id.bank_name);
                tv_updete = (TextView) view.findViewById(R.id.tv_updete);
                tv_del = (TextView) view.findViewById(R.id.tv_del);
                bank_name_def = (TextView) view.findViewById(R.id.bank_name_def);
                view.setTag(this);
            }
        }
    }

}
